package com.jydd.plug_in_study;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;

import com.jydd.plug_in_study.util.Util;

import java.lang.reflect.Field;

/**
 * created by 韦敏敏
 * on 2019/12/25
 */
public class ProxyHandlerCallback implements Handler.Callback {
    private Handler mBaseHandler;
    public ProxyHandlerCallback(Handler handler) {
        this.mBaseHandler = handler;
    }

    @Override
    public boolean handleMessage(Message msg) {
        Util.log("接受到了消息msg :" + msg.obj);
        if(msg.what == 100){
            Object obj = msg.obj;
            try {
                Field intentField = obj.getClass().getDeclaredField("intent");
                intentField.setAccessible(true);
                Intent intent = (Intent) intentField.get(obj);
                Intent targetIntent = intent.getParcelableExtra(TextActivity.TARGET_COMPONENT);
                intent.setComponent(targetIntent.getComponent());
                Util.log(targetIntent.toString());
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        mBaseHandler.handleMessage(msg);
        return true;
    }
}
